package com.cjg.netCode;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class fileServerNet {
    public static void main(String[] args) throws Exception {
        //创建服务
        ServerSocket serverSocket = new ServerSocket(8887);
        //监听客户端
        Socket accept = serverSocket.accept();

        //获取输入流
        InputStream inputStream = accept.getInputStream();
        //文件输出
        FileOutputStream re = new FileOutputStream(new File("999.jpg"));
        byte[] bytes = new byte[1024];
        int lent;
        while ((lent=inputStream.read(bytes))!=-1){
            re.write(bytes,0,lent);
        }



        //通知客户端接收完毕 ,可以断开连接

        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("接收完毕".getBytes(StandardCharsets.UTF_8));


        re.close(); inputStream.close(); accept.close();serverSocket.close();

    }
}
